"""
基于用户相似度的书籍推荐
"""
#创建字典嵌套字典以及列表，记录用户阅读过的书籍以及榜单书籍
book_data_dict = {"user1" :{"book":["红楼梦","西游记","三国演义"],
                            "book_recommend":"跟着诗词去旅行"},
                  "user2": {"book": ["红楼梦", "水浒传", "三国演义"],
                            "book_recommend": "二千年间"},
                  "user3": {"book": [ "西游记", "战争与和平"],
                            "book_recommend": "二千年间"},
                  "user4": {"book": ["红楼梦", "百年孤独"],
                            "book_recommend": "俗世奇人"},
                  "user5": {"book": [ "西游记", "三国演义"],
                            "book_recommend": "跟着诗词去旅行"},
                  }

def similarity(book_list)->dict:
    """
    计算用户相似度
    :param book_list: 小张输入自己阅读过的书(红楼梦，西游记)
    :return: 用户相似度
    """
    similarity_dict = {"user1":0,"user2":0,"user3":0,"user4":0,"user5":0}
    for book in book_list:
        for user in book_data_dict:
            similarity_dict[user] += book_data_dict[user]["book"].count(book)

    return similarity_dict

#用户输入
my_book_list = input("请输入您阅读过的书籍，中间以空格分割:>>").split(' ')
print(f"用户相似度：{similarity(my_book_list)}")

#找出与用户相似度最高的user
similarity_max = max(similarity(my_book_list).values())
print(f"最高相似度指数：{similarity_max}")

#把推荐的书籍封装为集合并输出
book_recommend_set = set()
for users in similarity(my_book_list):
    if similarity(my_book_list)[users] == similarity_max:
        book_recommend_set.add(book_data_dict[users]["book_recommend"])

print(f"推荐您阅读的书籍有：{book_recommend_set}")

